package com.collaboration.electron.controllers;

import com.collaboration.electron.entities.*;
import com.collaboration.electron.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.security.Principal;
import java.util.List;
import java.util.Optional;

/**
 * @author pinzi
 * @param
 * @return 无
 */

@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    CompositionService compositionService;

    @Autowired
    DetailedCompositionService detailedCompositionService;

    @Autowired
    BrandService brandService;

    @Autowired
    ClassificationService classificationService;

    @Autowired
    UserService userService;

    @GetMapping
    public String getProduct(Model model, Principal principal){
        String name = principal.getName();
        U u = userService.findByUserName(name);
        long Id = u.getUserId();

        List<Product> products=productService.findAllByUserId(Id);
        model.addAttribute("products",products);


        List<Composition> compositions=compositionService.getAll();
        model.addAttribute("compositions",compositions);

        List<Brand> brands=brandService.getAll();
        model.addAttribute("brands",brands);

        List<Classification> classifications=classificationService.getAll();
        model.addAttribute("classifications",classifications);

        List<DetailedComposition> detailedCompositions=detailedCompositionService.getAll();
        model.addAttribute("detailedCompositions",detailedCompositions);

        return "product";
    }

    @GetMapping
    public String deleteProduct(@RequestParam("id") long theId, Model model){

        Optional<Product> theProduct = productService.findByProductId(theId);
        ProductService.delete(theProduct);
        return "redirect:/save";

    }




}
